package com.wangyadong.hobby.javanetwork.output;

import java.io.*;
import java.net.InetAddress;
import java.util.Arrays;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/7/516:39
 **/
public class OutPutTest {
    public static void main(String[] args) throws IOException {

//        System.out.println(0 % 94);
//        System.out.println(1 % 94);
//        System.out.println(2 % 94);
//        System.out.println(10 % 94);
//        System.out.println(94 % 94);
//        System.out.println(100 % 94);
//        loopByMoudle();
//        OutputStream outputStream = new FileOutputStream("D://11.txt");
//        int to = 259;
//        byte b = (byte) to;
//        System.out.println("-->" + b);
////        outputStream.write(12);
////        outputStream.write(123);
//        outputStream.write(b);
//        generateCharacters(new FileOutputStream("D://11.txt"));
//
//        complement(-7);
//        complement(-177);
        input(new File("D://11.txt"));
        System.setProperty("networkaddress.cache.ttl", "1");//查询结果缓存时间
        System.setProperty("networkaddress.cache.negative.ttl", "1");//查询不成功的缓存时间
        InetAddress address = InetAddress.getByName("www.csdn.com");
        InetAddress[] addresss = InetAddress.getAllByName("www.baidu.com");
        InetAddress byName = InetAddress.getByName("127.0.0.1");
        System.out.println("根据ip 反向查找的域名为：" + byName.getHostName());
        System.out.println(" getHostName 获取主机名字 " + address.getHostName() + " IP 地址为 " + address.getHostAddress());
        Arrays.stream(addresss).forEach(i -> System.out.println(i.getHostAddress()));
        System.out.println("=============================");
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost.getCanonicalHostName());

        System.out.println("================================");


        byte[] bytes = {(byte) 788, (byte) 788, (byte) 788, (byte) 788};
        InetAddress byAddress = InetAddress.getByAddress("wangyadong", bytes);
        System.out.println(byAddress);
        byAddress.getCanonicalHostName();
        //可信地址
//        SecurityManager manager=new SecurityManager();
//        manager.checkConnect("www.csdn.com",80);
        getIpAddressVersion(localHost);
    }


    public static void generateCharacters(OutputStream out) throws IOException {
        int firstPrintableCharacter = 33;
        int numberOfPrintableChracters = 94;
        int numberOfCharactersPerLine = 72;
        int start = firstPrintableCharacter;
        //从第三十三个开始写入
        int k = 0;
        while (true) {
            System.out.println("=======start=============" + start);
            for (int i = start; i < start + numberOfCharactersPerLine; i++) {
                out.write(((i - firstPrintableCharacter) % numberOfPrintableChracters) + firstPrintableCharacter);
            }
            out.write(10);
            out.write('\r');//回车
            out.write('\n');// 换行
            start = ((start + 1) - firstPrintableCharacter % numberOfPrintableChracters + firstPrintableCharacter);
            System.out.println("==========end==========" + start);
            k++;
        }
    }


    public static void generateCharacters1(OutputStream out) throws IOException {
        int firstPrintableCharacter = 33;
        int numberOfPrintableChracters = 94;
        int numberOfCharactersPerLine = 72;
        int start = firstPrintableCharacter;
        //从第三十三个开始写入
        byte[] line = new byte[numberOfCharactersPerLine + 2];
        while (true) {
            System.out.println("=======start=============" + start);
            for (int i = start; i < start + numberOfCharactersPerLine; i++) {
                int i1 = ((i - firstPrintableCharacter) % numberOfPrintableChracters) + firstPrintableCharacter;
                line[i - start] = (byte) i1;
            }
            line[72] = ('\r');//回车
            line[73] = ('\n');// 换行
            out.write(line);
            start = ((start + 1) - firstPrintableCharacter % numberOfPrintableChracters + firstPrintableCharacter);
            System.out.println("==========end==========" + start);
        }
    }


    public static void generateCharactersMultiple(OutputStream out) throws IOException {
        int firstPrintableCharacter = 33;
        int numberOfPrintableChracters = 94;
        int numberOfCharactersPerLine = 72;
        int start = firstPrintableCharacter;
        byte[] line = new byte[numberOfCharactersPerLine + 2];
        //从第三十三个开始写入
        while (true) {
            for (int i = start; i < start + numberOfCharactersPerLine; i++) {
                line[i - start] = (byte) (((i - firstPrintableCharacter) % numberOfPrintableChracters) + firstPrintableCharacter);
            }
            line[72] = ('\r');//回车
            line[73] = ('\n');// 换行
            start = ((start + 1) - firstPrintableCharacter % numberOfPrintableChracters + firstPrintableCharacter);
        }
    }


    public static void loopByMoudle() {
        int first = 33;
        int totalExpertFirst = 94;
        int perline = 73;
        int start = first;
        for (int i = start; i < first + totalExpertFirst; i++) {
            System.out.println((i - first) % totalExpertFirst + first);
        }
    }

    private static void complement(int i) {
        int j = i >= 0 ? i : 256 + i;
        System.out.println(j);
    }

    public static void input(File file) {
        byte[] bytes = new byte[1024];
        int start = 0;
        int end = 1024;
        int index = 0;
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            while (true) {
                int read = fileInputStream.read();
                index++;
                if (read == -1) {
                    break;
                }
//                PrintStream stream=new PrintStream(new FileOutputStream("D://12.txt"));
//                stream.println(new String(bytes));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try (InputStreamReader reader = new InputStreamReader(new FileInputStream("D://11.txt"))) {

            while (true) {
                int read = reader.read();
                if (read == -1) {
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    private static void getIpAddressVersion(InetAddress address) {
        int length = address.getAddress().length;
        if (length == 4) {
            System.out.println("IPV4");
        } else if (length == 6) {
            System.out.println("IPV6");
        } else {
            System.out.println("没找到相关的版本");
        }
    }

}
